package model

import (
	"strconv"
	"time"
)

type DateWeather struct {
	Id int64 `xorm:"pk autoincr"`
	WeekName string
	Date string
	Weather string
	MaxTemperature string
	MinTemperature string
	AirQuality string
	Pm string
	WindDirection string
	Visibility string
	Pressure string
	timeTemperatureList []*TimeTemperature
	CreatedAt time.Time
	UpdatedAt time.Time
}

type TimeTemperature struct {
	Id int64 `xorm:"pk autoincr"`
	DateWeatherId int64
	TimeNow string
	Temperature string
	Weather string
}

func GetBetweenDates(sdate, edate string) []string {
	d := []string{}
	timeFormatTpl := "2006-01-02 15:04:05"
	if len(timeFormatTpl) != len(sdate) {
		timeFormatTpl = timeFormatTpl[0:len(sdate)]
	}
	date, err := time.Parse(timeFormatTpl, sdate)
	if err != nil {
		// 时间解析，异常
		return d
	}
	date2, err := time.Parse(timeFormatTpl, edate)
	if err != nil {
		// 时间解析，异常
		return d
	}
	if date2.Before(date) {
		// 如果结束时间小于开始时间，异常
		return d
	}
	// 输出日期格式固定
	timeFormatTpl = "2006-01-02"
	date2Str := date2.Format(timeFormatTpl)
	d = append(d, date.Format(timeFormatTpl))
	for {
		date = date.AddDate(0, 0, 1)
		dateStr := date.Format(timeFormatTpl)
		d = append(d, dateStr)
		if dateStr == date2Str {
			break
		}
	}
	return d
}

func ZellerWeek(year, month, day int) string {

	var weekday = [7]string{"周日", "周一", "周二", "周三", "周四", "周五", "周六"}

	var y, m, c int
	if month >= 3 {
		m = month
		y = year % 100
		c = year / 100
	} else {
		m = month + 12
		y = (year - 1) % 100
		c = (year - 1) / 100
	}

	week := y + (y / 4) + (c / 4) - 2*c + ((26 * (m + 1)) / 10) + day - 1
	if week < 0 {
		week = 7 - (-week)%7
	} else {
		week = week % 7
	}
	which_week := week
	return weekday[which_week]
}

func GetMonth(m string) int{
	switch m {
	case "January":
		return 1
	case "February":
		return 2
	case "March":
		return 3
	case "April":
		return 4
	case "May":
		return 5
	case "June":
		return 6
	case "July":
		return 7
	case "August":
		return 8
	case "September":
		return 9
	case "October":
		return 10
	case "November":
		return 11
	case "December":
		return 12
	}
	return 0
}

func DayWeatherForecast()([]*DateWeather ,error){

	var dwList []*DateWeather

	now := time.Now()

	lastOfMonth := now.AddDate(0, 0, 14)

	dayList := GetBetweenDates(now.Format("2006-01-02"), lastOfMonth.Format("2006-01-02"))
	for _, dlv := range dayList{
		t, err := time.Parse("2006-01-02", dlv)
		if err != nil{
			return nil, err
		}
		getWeek := ZellerWeek(t.Year(), GetMonth(t.Month().String()), t.Day())

		var dw DateWeather
		//x.Where("date = ?", dlv).Get(&dw)
		//x.Where("date_weather_id = ?", dw.Id).Find(dw.timeTemperatureList)
		dw.WeekName = getWeek
		dw.Date = dlv

		for j:=1; j <= 2; j++ {
			for i := 1; i <= 12; i++ {

				var tt TimeTemperature
				if j == 2 {
					tt.TimeNow = "下午" + strconv.Itoa(i) + ":00"
				}else {
					tt.TimeNow = "上午" + strconv.Itoa(i) + ":00"
				}

				dw.timeTemperatureList = append(dw.timeTemperatureList, &tt)
			}
		}

		dwList = append(dwList, &dw)
	}

	return dwList, nil
}

func InsertDateWeather(dwList []*DateWeather)(map[string]interface{}, error){

	for _, dw := range dwList {
		x.Insert(dw)
		for _, tt := range dw.timeTemperatureList {
			tt.DateWeatherId = dw.Id
			x.Insert(tt)
		}
	}

	return map[string]interface{}{"message":"成功","code":200}, nil
}





